package com.fand.platform.configuration.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.val;

import org.apache.commons.lang3.StringUtils;

import com.fand.dto.PaginationDto;
import com.fand.persistent.builder.ModelArgs;
import com.fand.persistent.builder.ModelBuilder;
import com.fand.persistent.builder.ModelBuilderUtils;
import com.fand.persistent.support.DaoJdbcSupport;
import com.fand.platform.configuration.dto.VariableDto;
import com.fand.platform.configuration.dto.VariableLoDto;

/**
 * 变量
 * 
 * @author yaoHT
 * 
 */
public class VariableJdbcDao extends DaoJdbcSupport implements VariableDao {

	@Override
	public int deleteVariableBy1(VariableDto deleteVariable, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(deleteVariable,
				whereEqProperties));

		return this.update("configuration/variable/deleteVariableBy1", modelBuilder.build());
	}

	@Override
	public int deleteVariableBy1_1(VariableDto deleteVariable) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(deleteVariable));

		return this.update("configuration/variable/deleteVariableBy1", modelBuilder.build());
	}

	@Override
	public int deleteVariableByIds(VariableDto deleteVariable) {
		val model = new HashMap<String, Object>();

		model.put("IDS", deleteVariable.getIds());

		return this.update("configuration/variable/deleteVariableByIds", model);
	}

	@Override
	public int deleteVariableLoBy1(VariableLoDto deleteVariableLo, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(deleteVariableLo,
				whereEqProperties));

		return this.update("configuration/variableLo/deleteVariableLoBy1", modelBuilder.build());
	}

	@Override
	public int insertVariableBy1(VariableDto insertVariable, String... insertProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.INSERT,
				insertVariable, insertProperties));
		return this.update("configuration/variable/insertVariableBy1", modelBuilder.build());
	}

	@Override
	public int insertVariableBy1_1(VariableDto insertVariable) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.INSERT,
				insertVariable));
		return this.update("configuration/variable/insertVariableBy1", modelBuilder.build());
	}

	@Override
	public int insertVariableBy2(VariableDto insertVariable) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.INSERT,
				insertVariable));

		return this.update("configuration/variable/insertVariableBy2", modelBuilder.build());
	}

	@Override
	public int insertVariableLoBy1(VariableLoDto insertVariableLo, String... insertProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.INSERT,
				insertVariableLo, insertProperties));
		return this.update("configuration/variableLo/insertVariableLoBy1", modelBuilder.build());
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationVariableBy1ForMapList(VariableDto paginationVariable) {
		val model = new HashMap<String, Object>();

		model.put("TYPE_ID", paginationVariable.getTypeId());
		model.put("CODE", paginationVariable.getCode());

		return this.paginationForMapList("configuration/variable/selectVariableBy2ForObject",
				"configuration/variable/selectVariableBy2ForMappedObjectList", paginationVariable.getPaginationNo(),
				paginationVariable.getPaginationSize(), model);
	}

	@Override
	public <R> PaginationDto<R> paginationVariableBy1ForMappedObjectList(Class<R> mappedClass,
			VariableDto paginationVariable) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(paginationVariable));

		return this.paginationForMappedObjectList(mappedClass, "configuration/variable/selectVariableBy1ForObject",
				"configuration/variable/selectVariableBy1ForMappedObjectList", paginationVariable.getPaginationNo(),
				paginationVariable.getPaginationSize(), modelBuilder.build());
	}

	@Override
	public <R> R selectVariableBy1_1ForMappedObject(Class<R> mappedClass, VariableDto selectVariable) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable));

		return this.selectForMappedObject(mappedClass,
				"configuration/variable/selectVariableBy1ForMappedObjectList", modelBuilder.build());
	}

	@Override
	public <R> List<R> selectVariableBy1_1ForObjectList(Class<R> requiredType, VariableDto selectVariable,
			String objectId) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable));

		return this.selectForObjectList(requiredType, "configuration/variable/selectVariableBy1ForObject", objectId,
				modelBuilder.build());
	}

	@Override
	public <R> R selectVariableBy1ForMappedObject(Class<R> mappedClass, VariableDto selectVariable,
			String[] selectProperties, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable,
				whereEqProperties));

		modelBuilder.putTrueExtend(ModelBuilder.SELECT, selectProperties);

		return this.selectForMappedObject(mappedClass,
				"configuration/variable/selectVariableBy1ForMappedObjectList", modelBuilder.build());
	}

	@Override
	public <R> List<R> selectVariableBy1ForMappedObjectList(Class<R> mappedClass, VariableDto selectVariable,
			String[] selectProperties, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable,
				whereEqProperties));

		modelBuilder.putTrueExtend(ModelBuilder.SELECT, selectProperties);

		return this.selectForMappedObjectList(mappedClass,
				"configuration/variable/selectVariableBy1ForMappedObjectList", modelBuilder.build());
	}

	@Override
	public <R> R selectVariableBy1ForObject(Class<R> requiredType, VariableDto selectVariable, String objectId,
			String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable,
				whereEqProperties));

		return this.selectForObject(requiredType, "configuration/variable/selectVariableBy1ForObject", objectId,
				modelBuilder.build());
	}

	@Override
	public <R> List<R> selectVariableBy1ForObjectList(Class<R> requiredType, VariableDto selectVariable,
			String objectId, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable,
				whereEqProperties));

		return this.selectForObjectList(requiredType, "configuration/variable/selectVariableBy1ForObject", objectId,
				modelBuilder.build());
	}

	@Override
	public <R> List<R> selectVariableBy2ForMappedObjectList(Class<R> mappedClass, VariableDto selectVariable) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable));

		return this.selectForMappedObjectList(mappedClass,
				"configuration/variable/selectVariableBy2ForMappedObjectList", modelBuilder.build());
	}

	@Override
	public <R> R selectVariableBy2ForObject(Class<R> requiredType, VariableDto selectVariable, String objectId) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable));

		return this.selectForObject(requiredType, "configuration/variable/selectVariableBy2ForObject", objectId,
				modelBuilder.build());
	}

	@Override
	public <R> List<R> selectVariableBy2ForObjectList(Class<R> requiredType, VariableDto selectVariable,
			String objectId) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable));

		return this.selectForObjectList(requiredType, "configuration/variable/selectVariableBy2ForObject", objectId,
				modelBuilder.build());
	}

	@Override
	public <R> R selectVariableByIdForMappedObject(Class<R> mappedClass, VariableDto selectVariable) {
		val model = new HashMap<String, Object>();

		model.put("ID", selectVariable.getId());

		return this.selectForMappedObject(mappedClass,
				"configuration/variable/selectVariableByIdForMappedObjectList", model);
	}

	@Override
	public <R> List<R> selectVariableByTypeCoForMappedObjectList(Class<R> mappedClass, String typeCo) {
		val model = new HashMap<String, Object>();

		model.put("TYPE_CO", typeCo);

		return this.selectForMappedObjectList(mappedClass,
				"configuration/variable/selectVariableByTypeCoForMappedObjectList", model);
	}

	@Override
	public Integer selectVariableByTypeIdEqCodeNqIdForInteger(VariableDto selectVariable) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariable, "typeId",
				"code"));

		if (StringUtils.isNotBlank(selectVariable.getId())) {
			modelBuilder.putExtend(ModelBuilder.WHERE__AND__NQ, "id", null, selectVariable.getId());
		}

		return this.selectForObject(Integer.class, "configuration/variable/selectVariableBy1ForObject", null,
				modelBuilder.build());
	}

	@Override
	public <R> R selectVariableLoBy1ForMappedObject(Class<R> mappedClass, VariableLoDto selectVariableLo,
			String[] selectProperties, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariableLo,
				whereEqProperties));

		modelBuilder.putTrueExtend(ModelBuilder.SELECT, selectProperties);

		return this.selectForMappedObject(mappedClass,
				"configuration/variableLo/selectVariableLoBy1ForMappedObjectList", modelBuilder.build());
	}

	@Override
	public <R> List<R> selectVariableLoBy1ForMappedObjectList(Class<R> mappedClass, VariableLoDto selectVariableLo,
			String[] selectProperties, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariableLo,
				whereEqProperties));

		modelBuilder.putTrueExtend(ModelBuilder.SELECT, selectProperties);

		return this.selectForMappedObjectList(mappedClass,
				"configuration/variableLo/selectVariableLoBy1ForMappedObjectList", modelBuilder.build());
	}

	@Override
	public <R> R selectVariableLoBy1ForObject(Class<R> requiredType, VariableLoDto selectVariableLo,
			String objectId, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariableLo,
				whereEqProperties));

		return this.selectForObject(requiredType, "configuration/variableLo/selectVariableLoBy1ForObject", objectId,
				modelBuilder.build());
	}

	@Override
	public <R> List<R> selectVariableLoBy1ForObjectList(Class<R> requiredType, VariableLoDto selectVariableLo,
			String objectId, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(selectVariableLo,
				whereEqProperties));

		return this.selectForObjectList(requiredType, "configuration/variableLo/selectVariableLoBy1ForObject",
				objectId, modelBuilder.build());
	}

	@Override
	public int updateVariableBy1(VariableDto updateVariable, String[] updateProperties, String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.UPDATE,
				updateVariable, updateProperties), new ModelArgs(updateVariable, whereEqProperties));

		return this.update("configuration/variable/updateVariableBy1", modelBuilder.build());
	}

	@Override
	public int updateVariableBy1_1(VariableDto updateVariable) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(updateVariable));

		return this.update("configuration/variable/updateVariableBy1", modelBuilder.build());
	}

	@Override
	public int updateVariableBy2(VariableDto updateVariable) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(updateVariable));

		return this.update("configuration/variable/updateVariableBy2", modelBuilder.build());
	}

	@Override
	public int updateVariableLoBy1(VariableLoDto updateVariableLo, String[] updateProperties,
			String... whereEqProperties) {
		final ModelBuilder modelBuilder = ModelBuilderUtils.newModelBuilder(new ModelArgs(ModelBuilder.UPDATE,
				updateVariableLo, updateProperties), new ModelArgs(updateVariableLo, whereEqProperties));

		return this.update("configuration/variableLo/updateVariableLoBy1", modelBuilder.build());
	}
}